CLASS lhc_ymoon_i_api020 DEFINITION INHERITING FROM cl_abap_behavior_handler.
  PRIVATE SECTION.
    TYPES:
        tt_tb TYPE STANDARD TABLE OF ymoont_api020.

    METHODS get_instance_authorizations FOR INSTANCE AUTHORIZATION
      IMPORTING keys REQUEST requested_authorizations FOR ymoon_i_api020 RESULT result.
    METHODS call_api FOR MODIFY
      IMPORTING keys FOR ACTION ymoon_i_api020~call_api.
    METHODS call_out_api EXPORTING out_tab TYPE tt_tb search_str TYPE string.

ENDCLASS.

CLASS lhc_ymoon_i_api020 IMPLEMENTATION.

  METHOD get_instance_authorizations.
  ENDMETHOD.

  METHOD call_api.
    DATA: lt_out TYPE tt_tb.
    DATA:lv_search TYPE string.

    DATA:lt_crt TYPE TABLE FOR CREATE ymoon_i_api020,
         ls_crt LIKE LINE OF lt_crt.
    DATA ls_read TYPE STRUCTURE FOR READ RESULT ymoon_i_api020.


    DATA(key) = keys[ 1 ].
    DATA(param) = key-%param.

*    lv_search = '21EASK14G1I60490001'.
    call_out_api( IMPORTING out_tab = lt_out search_str = param-cargmtno ).

    IF lt_out IS NOT INITIAL.
        SELECT UUID FROM ymoont_api020  WHERE cargmtno = @param-cargmtno INTO TABLE @DATA(LT_UUID).
        LOOP AT LT_UUID INTO DATA(LS_UUID).
            MODIFY ENTITIES OF ymoon_i_api020 IN LOCAL MODE
            ENTITY ymoon_i_api020
            DELETE FROM VALUE #( ( UUID = LS_UUID-UUID ) ).
        ENDLOOP.
    ENDIF.

*    modify ymoont_api020 from TABLE @lt_out.
    LOOP AT lt_out INTO DATA(ls_out).
      MOVE-CORRESPONDING ls_out TO ls_crt.
      ls_crt-%cid = cl_system_uuid=>create_uuid_x16_static( ).
      ls_crt-uuid = ls_crt-%cid.
      ls_crt-regid = SY-UNAME.
      ls_crt-regdate = sy-datum.
      ls_crt-cargmtno =  param-cargmtno.


      MODIFY ENTITIES OF ymoon_i_api020 IN LOCAL MODE
      ENTITY ymoon_i_api020
      CREATE FIELDS ( uuid regid regdate cntrno cntrstszcd cntrselgno1 cntrselgno2 cntrselgno3 cargmtno )
        WITH VALUE #( (
            %cid = ls_crt-%cid
            uuid = ls_crt-uuid
            regid = ls_crt-regid
            regdate = ls_crt-regdate
            cntrno = ls_crt-cntrno
            cntrstszcd = ls_crt-cntrstszcd
            cntrselgno1 = ls_crt-cntrselgno1
            cntrselgno2 = ls_crt-cntrselgno2
            cntrselgno3 = ls_crt-cntrselgno3
            cargmtno = ls_crt-cargmtno

        ) ).

      APPEND ls_crt TO lt_crt.
      MOVE-CORRESPONDING ls_crt TO ls_read.
*      IF result IS INITIAL.
*        APPEND VALUE #( %cid  = key-%cid %param = ls_read ) TO result.
*      ENDIF.
    ENDLOOP.

*    DATA: lt_read TYPE TABLE FOR READ RESULT ymoon_i_api020.
*
*    READ ENTITIES OF ymoon_i_api020 IN LOCAL MODE
*      ENTITY ymoon_i_api020
*      ALL FIELDS WITH
*      CORRESPONDING #( keys )
*      RESULT DATA(read_020).
*
*    result = VALUE #( FOR line IN read_020 (  %cid   = line-%cid
*                                              %param = line ) ).
*

  ENDMETHOD.

  METHOD call_out_api.
    DATA:http_client TYPE REF TO ymoon_api.
    CREATE OBJECT http_client
      EXPORTING
        i_scenario     = 'YMOON_CS_API020'
        i_service      = 'YMOON_OS_API020_REST'
      EXCEPTIONS
        no_arrangement = 1.
    IF sy-subrc = 1.
      "out->write( 'no arrangement' ).
    ENDIF.
    CHECK sy-subrc <> 1.

    DATA param TYPE string.
    DATA res TYPE string.
    param = 'crkyCn=k200n178y018k289m050l070f1&cargMtNo=' && search_str.

    res = http_client->get( param ).


    DATA: lv_xstring TYPE xstring.
    DATA: lv_conv TYPE REF TO cl_abap_conv_codepage.
    lv_xstring = cl_abap_conv_codepage=>create_out( codepage = `UTF-8` )->convert( source = res ).
    "out->write( lv_xstring ).

    DATA:BEGIN OF ls_data,
           cntrno      TYPE c LENGTH 11,
           cntrstszcd  TYPE c LENGTH 4,
           cntrselgno1 TYPE c LENGTH 60,
           cntrselgno2 TYPE c LENGTH 60,
           cntrselgno3 TYPE c LENGTH 60,
           cargmtno    TYPE c LENGTH 19,
         END OF ls_data.
    DATA:lt_data LIKE TABLE OF ls_data.

    DATA:gv_cond TYPE c LENGTH 50 VALUE 'cntrQryBrkdQryVo'.
    DATA:gv_field TYPE c LENGTH 20.
    FIELD-SYMBOLS <fs> TYPE any.


    DATA: lv_reader          TYPE REF TO if_sxml_reader.
    DATA: lv_node            TYPE REF TO if_sxml_node.
    DATA: lv_open_node       TYPE REF TO if_sxml_open_element.
    DATA: lv_close_node      TYPE REF TO if_sxml_close_element.
    DATA: lv_value_node      TYPE REF TO if_sxml_value.
    DATA: lv_value           TYPE string.

    lv_reader = cl_sxml_string_reader=>create( lv_xstring ).

    lv_node = lv_reader->read_next_node( ).


    ASSERT lv_node IS NOT INITIAL.
    WHILE lv_reader->node_type <> if_sxml_node=>co_nt_final.
      CASE lv_reader->node_type.
        WHEN if_sxml_node=>co_nt_element_open.
          lv_open_node  ?= lv_node.

          gv_field = 'ls_data-' && lv_open_node->qname-name.

        WHEN if_sxml_node=>co_nt_element_close.
          lv_close_node ?= lv_node.

          IF lv_close_node->qname-name = gv_cond.
            APPEND ls_data TO lt_data.
            CLEAR ls_data.
          ELSE.
            ASSIGN (gv_field) TO <fs>.
            IF sy-subrc = 0.
              <fs> = lv_value.
            ENDIF.
          ENDIF.

          CLEAR lv_value.

        WHEN if_sxml_node=>co_nt_value.
          lv_value_node ?= lv_node.
          lv_value = lv_value_node->get_value( ).
        WHEN if_sxml_node=>co_nt_attribute.
      ENDCASE.
      lv_node = lv_reader->read_next_node( ).
    ENDWHILE.

    MOVE-CORRESPONDING lt_data TO out_tab .



  ENDMETHOD.

ENDCLASS.
